﻿using System.Collections.Generic;
using MaxStoreErp.Core.Exceptions;
using MaxStoreShop.Domain.Directories;
using MaxStoreShop.Logic.RepositoriesContracts.Directories;
using MaxStoreShop.Logic.ServicesContracts.Directories;

namespace MaxStoreShop.Logic.Services.Directories
{
    public class SupplierService : ISupplierService
    {
        readonly ISupplierRepository _repository;

        public SupplierService(ISupplierRepository repository)
        {
            _repository = repository;
        }

        public IEnumerable<Supplier> GetSuppliers()
        {
            return _repository.GetSuppliers();
        }

        public void DeleteSupplier(Supplier supplier)
        {
            if (supplier == null)
                throw new ErpRuntimeException("Не указан поставщик для удаления!");

            _repository.DeleteSupplier(supplier.Id);
        }

        public void SaveSupplier(Supplier supplier)
        {
            if (supplier == null)
                throw new ErpRuntimeException("Не указан поставщик для сохранения!");

            if (string.IsNullOrWhiteSpace(supplier.Name))
                throw new ErpRuntimeException("Не указано наименование сохраняемого поставщика!");

            if (string.IsNullOrWhiteSpace(supplier.Phone))
                throw new ErpRuntimeException("Не указан контактный телефон сохраняемого поставщика!");

            _repository.SaveSupplier(supplier);
        }
    }
}
